package com.winderinfo.yikaotianxia.ui.home;

import android.content.Context;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.winderinfo.yikaotianxia.R;
import com.winderinfo.yikaotianxia.core.api.Api;
import com.winderinfo.yikaotianxia.core.base.BaseFragment;
import com.winderinfo.yikaotianxia.core.mvp.Contract;
import com.winderinfo.yikaotianxia.core.mvp.Presenter;
import com.winderinfo.yikaotianxia.core.utils.PageAdapterUtil;
import com.winderinfo.yikaotianxia.ui.banxing.ClassTypeActivity;
import com.winderinfo.yikaotianxia.ui.jiaocai.TeachingMaterial;
import com.winderinfo.yikaotianxia.ui.kecheng.CurriculumActivity;
import com.winderinfo.yikaotianxia.ui.xiangqin.DetailsActivity;
import com.winderinfo.yikaotianxia.ui.zhuanye.MajorIntroduceActivity;
import com.winderinfo.yikaotianxia.ui.zhuanye.MajorList;
import com.winderinfo.yikaotianxia.ui.zixun.InformationActivity;
import com.winderinfo.yikaotianxia.ui.zixun.InformationXiangqin;
import com.youth.banner.Banner;
import com.youth.banner.listener.OnBannerListener;
import com.youth.banner.loader.ImageLoader;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

public class HomeFragment extends BaseFragment implements Contract.View {

    Unbinder unbinder;

    @BindView(R.id.home_bx)
    RecyclerView homeBx;
    @BindView(R.id.home_yk)
    RecyclerView homeYk;
    @BindView(R.id.home_banner)
    Banner homeBanner;
    @BindView(R.id.home_tou)
    RecyclerView homeTou;
    @BindView(R.id.home_jc)
    RecyclerView homeJc;
    @BindView(R.id.home_tab)
    TabLayout homeTab;
    @BindView(R.id.home_pager)
    ViewPager homePager;

    private Presenter presenter;
    private List<LunboBean.RowsBean> lunbo;
    Map<String, Object> map = new HashMap<>();

    @Override
    protected int initLayoutId() {
        return R.layout.fragment_home;
    }

    @Override
    protected void initView(View view) {
        //实例P
        presenter = new Presenter(this);


        //设置recycleview布局管理器
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
        linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        homeBx.setLayoutManager(linearLayoutManager);
        homeYk.setLayoutManager(new GridLayoutManager(getActivity(), 2));
        homeTou.setLayoutManager(new LinearLayoutManager(getActivity()));
        LinearLayoutManager jiaocai = new LinearLayoutManager(getActivity());
        jiaocai.setOrientation(LinearLayoutManager.HORIZONTAL);
        homeJc.setLayoutManager(jiaocai);
        //fragment集合
        List<Fragment> fragments = new ArrayList<>();
        ReMenFragment reMenFragment = new ReMenFragment();
        MianFieFragment mianFieFragment = new MianFieFragment();
        fragments.add(reMenFragment);
        fragments.add(mianFieFragment);
        //TabLayout标题
        List<String> tabList = new ArrayList<>();
        tabList.add("热门课程");
        tabList.add("免费课程");
        //设置适配器
        homePager.setAdapter(new PageAdapterUtil(getChildFragmentManager(), fragments, tabList));
        homeTab.setupWithViewPager(homePager);
    }

    @Override
    protected void initData() {
        //请求轮播数据
        presenter.onPostRequest(Api.HOME_LUNBO, null, LunboBean.class);
    }

    //数据请求成功
    @Override
    public void onSuccess(Object data) {
        //轮播图数据
        if (data instanceof LunboBean) {
            //请求头条的数据
            presenter.onPostRequest(Api.HOME_TOUTIAO, null, ToutiaosBean.class);
            //
            lunbo = ((LunboBean) data).getRows();
            //图片地址集合
            List<String> bannerList = new ArrayList<>();
            //获取集合中的图片地存入新的集合
            for (int i = 0; i < lunbo.size(); i++) {
                bannerList.add(lunbo.get(i).getUrl());
            }
            //设置轮播的方法
            setBannerUrl(bannerList);
        }

        //头条数据
        if (data instanceof ToutiaosBean) {
            //请求班型数据
            /**
             * @type 请求数据类型 1班型 3教材
             * */
            map.clear();
            map.put("type", 1);
            presenter.onPostRequest(Api.HOME_BANXING, map, BanXingBean.class);
            //
            List<ToutiaosBean.ToutiaoBean> toutiao = ((ToutiaosBean) data).getToutiao();
            //设置适配器
            TouTiaoAdapter touTiaoAdapter = new TouTiaoAdapter(getActivity(), toutiao);
            homeTou.setAdapter(touTiaoAdapter);
            //商品ID
            touTiaoAdapter.setOnId(new TouTiaoAdapter.OnId() {
                @Override
                public void getId(int id) {
                    Bundle bundle = new Bundle();
                    bundle.putInt("zxId", id);
                    openActivity(InformationXiangqin.class, bundle);
                }
            });
        }

        //请求班型数据
        if (data instanceof BanXingBean) {
            //请求专业数据
            /**
             * @pageNum 页码
             * @pageSize 每页数量
             * */
            map.clear();
            map.put("pageNum", 1);
            map.put("pageSize", 4);
            presenter.onPostRequest(Api.HOME_ZHUANYE, map, ZhuanYeBean.class);
            //
            List<BanXingBean.RowsBean> bx = ((BanXingBean) data).getRows();
            //设置适配器
            BanXingAdapter banXingAdapter = new BanXingAdapter(getActivity(), bx);
            homeBx.setAdapter(banXingAdapter);
            //商品ID
            banXingAdapter.setOnBId(new BanXingAdapter.OnBId() {
                @Override
                public void getId(int id, String type) {
                    Bundle bundle = new Bundle();
                    bundle.putInt("id", id);
                    bundle.putString("type", type);
                    openActivity(DetailsActivity.class, bundle);
                }
            });
        }
        //专业数据
        if (data instanceof ZhuanYeBean) {
            //请求教材数据
            /**
             * @type 请求数据类型 1班型 3教材
             * */
            map.clear();
            map.put("type", 3);
            presenter.onPostRequest(Api.HOME_BANXING, map, JiaoCaiBean.class);
            //
            List<ZhuanYeBean.RowsBean> rows = ((ZhuanYeBean) data).getRows();
            //设置适配器
            ZhuanYeAdapter zhuanYeAdapter = new ZhuanYeAdapter(getActivity(), rows);
            homeYk.setAdapter(zhuanYeAdapter);
            //
            zhuanYeAdapter.setOnZId(new ZhuanYeAdapter.OnZId() {
                @Override
                public void getId(int id) {
                    openActivity(MajorIntroduceActivity.class);
                }
            });
        }
        //教材数据
        if (data instanceof JiaoCaiBean) {
            List<JiaoCaiBean.RowsBean> rows = ((JiaoCaiBean) data).getRows();
            //设置适配器
            JiaoCaiAdapter jiaoCaiAdapter = new JiaoCaiAdapter(getActivity(), rows);
            homeJc.setAdapter(jiaoCaiAdapter);
            //商品ID
            jiaoCaiAdapter.setOnZId(new JiaoCaiAdapter.OnJId() {
                @Override
                public void getId(int id, String type) {
                    Bundle bundle = new Bundle();
                    bundle.putInt("id", id);
                    bundle.putString("type", type);
                    openActivity(DetailsActivity.class, bundle);
                }
            });
        }
    }

    //数据请求失败
    @Override
    public void onFailure(Object error) {

    }

    //设置轮播图
    private void setBannerUrl(List<String> bannerList) {

        //设置图片加载器
        homeBanner.setImageLoader(new ImageLoader() {
            @Override
            public void displayImage(Context context, Object path, ImageView imageView) {
                Glide.with(context).load(path).into(imageView);
            }
        });
        //设置图片集合
        homeBanner.setImages(bannerList).setDelayTime(3000).start();

        //轮播图点击事件
        homeBanner.setOnBannerListener(new OnBannerListener() {
            @Override
            public void OnBannerClick(int position) {
                //获取id跳到详情
                Bundle bundle = new Bundle();
                bundle.putInt("id", lunbo.get(position).getId());
                // openActivity(DetailsActivity.class, bundle);
            }
        });
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        presenter.onUntying();//解绑
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        unbinder = ButterKnife.bind(this, rootView);
        return rootView;
    }

    @OnClick({R.id.home_banxing, R.id.home_zhuanye, R.id.home_kecheng, R.id.home_jiaocai, R.id.home_zixun, R.id.home_mobx, R.id.home_mozy, R.id.home_mojc})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.home_banxing:
                openActivity(ClassTypeActivity.class);
                break;
            case R.id.home_zhuanye:
                openActivity(MajorList.class);
                break;
            case R.id.home_kecheng:
                openActivity(CurriculumActivity.class);
                break;
            case R.id.home_jiaocai:
                openActivity(TeachingMaterial.class);
                break;
            case R.id.home_zixun:
                openActivity(InformationActivity.class);
                break;
            case R.id.home_mobx:
                openActivity(ClassTypeActivity.class);
                break;
            case R.id.home_mozy:
                openActivity(MajorList.class);
                break;
            case R.id.home_mojc:
                openActivity(TeachingMaterial.class);
                break;
        }
    }

}
